﻿using DLL.NET50.DB.MS.AdviceAndDrug;
using DLL.NET60.DB.MS.Entity.DTO;
using DLL.NetStandard.FlushRobot.MS.Entity.Param;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Windows;

namespace DLL.FlushRobot.MS.Run
{
    //解析文件类
    public class CMS_AnalyFile
    {
        protected CMS_AdviceAndDrugs advices = new CMS_AdviceAndDrugs();//传递过来的医嘱
        private CMS_DeviceConfigParam DeviceConfig = new CMS_DeviceConfigParam();//设备参数
        private CMS_WorkPointParam WorkPoint = new CMS_WorkPointParam();//加工点位
        protected const int MaxRowPointCount = 8;
        protected const int MaxColPointCount = 8;

        [Unity.Dependency]
        public IDialogService _dialogService { get; set; }

        public CMS_AnalyFile()
        {
        }

        public CMS_AnalyFile(CMS_AdviceAndDrugs inadvices, CMS_WorkPointParam workPoint, CMS_DeviceConfigParam deviceConfig)
        {
            advices = (CMS_AdviceAndDrugs)inadvices.Clone();
            WorkPoint = workPoint;
            DeviceConfig = deviceConfig;
        }

        #region 整只配药模式

        //获取医嘱药品数量
        protected int GetDrugCountOfCurrentAdvice(CMS_AdviceAndDrug advcie)
        {
            int drugCount = Convert.ToInt32(advcie.GetDrugCount());
            return drugCount;
        }

        //获取最多药品数量
        protected int GetMaxDurgCount()
        {
            List<CMS_AdviceAndDrug> l_advice = advices.adviceAndDrugs;
            int ColCount = l_advice.Count;//获取有几列药品要充沛
            //第一步获取药品最大的数量
            int MaxDrugCountOfAllCol = 0;
            for (int col = 0; col < ColCount; col++)
            {
                //循环每一列,也就是一个处方
                CMS_AdviceAndDrug advice = l_advice[col];
                int DrugCountOfCurrentAdvice = 0;
                DrugCountOfCurrentAdvice = GetDrugCountOfCurrentAdvice(advice);//获取需要配置药品的总个数
                if (col == 0)
                {
                    MaxDrugCountOfAllCol = DrugCountOfCurrentAdvice;
                }
                else
                {
                    if (MaxDrugCountOfAllCol < DrugCountOfCurrentAdvice)
                    {
                        //如果当前的药品数量大于上一次记录的，则将当前的值保存
                        MaxDrugCountOfAllCol = DrugCountOfCurrentAdvice;
                    }
                }
            }
            return MaxDrugCountOfAllCol;
        }

        protected int GetLoopCount(int DrugCount)
        {
            int loopCount = 0;
            int b = DrugCount / DeviceConfig.StationRowNum;
            int a = DrugCount % DeviceConfig.StationRowNum;//求余数
            if (a == 0)
            {
                loopCount = b;
            }
            else
            {
                loopCount = b + 1;
            }
            return loopCount;
        }

        protected void InitilizeMulLoopRowColStation(ref CMultyLoopStations mulLoopStations, int maxDrugCount)
        {
            int loopCount = maxDrugCount / DeviceConfig.StationRowNum;
            int SurplusDrug = maxDrugCount % DeviceConfig.StationRowNum;//求余数

            mulLoopStations.MultyLoopStation.Clear();
            for (int loop = 0; loop < loopCount; loop++)
            {
                COneLoopStations oneLoopStation = new COneLoopStations();
                oneLoopStation.DeviceConfig = DeviceConfig;
                InitilizeOneLoopRowColStation(ref oneLoopStation);//初始化个8行8列的数组
                oneLoopStation.MaxDrugCount = DeviceConfig.StationRowNum;
                oneLoopStation.StationColNum = DeviceConfig.StationColNum;
                mulLoopStations.MultyLoopStation.Add(oneLoopStation);
            }
            if (SurplusDrug > 0)
            {
                for (int loop = loopCount; loop < loopCount + 1; loop++)
                {
                    COneLoopStations oneLoopStation = new COneLoopStations();
                    oneLoopStation.DeviceConfig = DeviceConfig;
                    InitilizeOneLoopRowColStation(ref oneLoopStation);//初始化个8行8列的数组
                    oneLoopStation.MaxDrugCount = SurplusDrug;
                    mulLoopStations.MultyLoopStation.Add(oneLoopStation);
                }
            }
            //这个时候初始化的数据都是相同的
        }

        protected void InitilizeOneLoopRowColStation(ref COneLoopStations oneLoopStation)
        {
            double[] srowStaion = new double[MaxRowPointCount];
            srowStaion[0] = WorkPoint.X_StationPos0;//抽溶媒位置
            srowStaion[1] = WorkPoint.X_StationPos1;//小药位置1
            srowStaion[2] = WorkPoint.X_StationPos2;//小药位置2
            srowStaion[3] = WorkPoint.X_StationPos3;//小药位置3
            srowStaion[4] = WorkPoint.X_StationPos4;//小药位置4
            //初始化每个站台的X,Y坐标
            for (int row = 0; row < MaxRowPointCount; row++)
            {
                COneRowStation rowStation = InitializeOneRow(srowStaion[row]);
                rowStation.RowID = row;
                rowStation.DeviceConfig = DeviceConfig;
                rowStation.StationColNum = DeviceConfig.StationColNum;
                oneLoopStation.StationColNum = DeviceConfig.StationColNum;
                oneLoopStation.MultyRowStation[row] = rowStation;
            }
        }

        public COneRowStation InitializeOneRow(double XPos)
        {
            COneRowStation rowStation = new COneRowStation();
            rowStation.DeviceConfig = DeviceConfig;
            for (int col = 0; col < MaxColPointCount; col++)
            {
                //初始化一行数据
                CSingleStation lStation = new CSingleStation();
                InitilizeSingleStation(ref lStation);
                rowStation.XPos = XPos;
                rowStation.StationColNum = DeviceConfig.StationColNum;
                rowStation.RowStations[col] = lStation;
            }
            return rowStation;
        }

        //初始化一个站台信息
        protected void InitilizeSingleStation(ref CSingleStation pStation)
        {
            //初始化每个站台的X,Y坐标
            CSingleStation lStation = new CSingleStation();
            lStation.UsedFlag = 0;
            pStation = lStation;
        }

        public int AnalysisFlie(ref CMultyLoopStations multyLoop)
        {
            #region 调试代码

            //#region 测试数据代码 dose=400
            //advices.adviceAndDrugs[0].adviceDrug[0].QUANTITY = 1;
            //advices.adviceAndDrugs[0].adviceDrug[0].REAL_QUANTITY = 1;
            //advices.adviceAndDrugs[0].adviceDrug[0].REALDOSE = 200;

            //advices.adviceAndDrugs[1].adviceDrug[0].QUANTITY = 1;
            //advices.adviceAndDrugs[1].adviceDrug[0].REAL_QUANTITY = 1;
            //advices.adviceAndDrugs[1].adviceDrug[0].REALDOSE = 400;

            //advices.adviceAndDrugs[2].adviceDrug[0].QUANTITY = 2;
            //advices.adviceAndDrugs[2].adviceDrug[0].REAL_QUANTITY = 2;
            //advices.adviceAndDrugs[2].adviceDrug[0].REALDOSE = 600;

            //advices.adviceAndDrugs[3].adviceDrug[0].QUANTITY = 4;
            //advices.adviceAndDrugs[3].adviceDrug[0].REAL_QUANTITY = 4;
            //advices.adviceAndDrugs[3].adviceDrug[0].REALDOSE = 1600;

            //#endregion

            #endregion 调试代码

            int firtCanUsedStationID = GetFirstCanUsedStationID();
            //设备行数
            List<CMS_AdviceAndDrug> l_advice = advices.adviceAndDrugs;
            int ColCount = l_advice.Count;//获取有几列药品要充沛
            //第一步获取药品最大的数量
            int maxDrugCountOfAllCol = GetMaxDurgCount();

            int drugType = Convert.ToInt32(advices.adviceAndDrugs[firtCanUsedStationID].Drugs[0].SOLVENT_FLAG);
            if (2 == drugType)
            {
                //专用溶媒模式
                maxDrugCountOfAllCol = maxDrugCountOfAllCol * 2;
            }
            //第二步，初始化list变量,这个时候就已经决定了药循环配药几次
            InitilizeMulLoopRowColStation(ref multyLoop, maxDrugCountOfAllCol);
            for (int col = 0; col < ColCount; col++)
            {
                //循环每一列,也就是一个处方
                if (l_advice[col].Enable == 0)
                {
                    continue;
                }
                CMS_AdviceAndDrug advice = (CMS_AdviceAndDrug)l_advice[col].Clone();
                int DrugCountOfAdvice = 0;//当前医嘱中，药品的数量
                DrugCountOfAdvice = GetDrugCountOfCurrentAdvice(advice);//获取该医嘱中需要配置药品的总个数
                drugType = Convert.ToInt32(advice.Drugs[0].SOLVENT_FLAG);
                if (2 == drugType)
                {
                    //专用溶媒模式
                    DrugCountOfAdvice = DrugCountOfAdvice * 2;
                }
                int loopCount = DrugCountOfAdvice / DeviceConfig.StationRowNum;//需要的循环次数
                int SurplusDrug = DrugCountOfAdvice % DeviceConfig.StationRowNum;//最后一次循环药品的数量
                if (SurplusDrug > 0)
                {
                    loopCount = loopCount + 1;
                }

                multyLoop.UsedDrug = (DTO_adi_dic_drug)advice.Drugs[0].Clone();
                multyLoop.UsedSolvent = (DTO_adi_dic_drug)advice.Solvent.Clone();
                {
                    if (l_advice[col].Drugs.Count != l_advice[col].DrugCountOfOneType.Count)
                    {
                        //MessageBox.Show("配药医嘱解析失败！无法配药！");
                        string str = "配药医嘱解析失败！无法配药！";
                        Application.Current.Dispatcher.Invoke(
                        (Action)delegate ()
                        {
                            _dialogService.ShowDialog("WarningDialog", new DialogParameters($"message={str}"), null);
                        });
                        return 0;
                    }
                    List<DTO_adi_dic_drug> ldrugs = GetDrugs(l_advice[col].Drugs, l_advice[col].DrugCountOfOneType);
                    //同一列，每一行的药品都是一样的
                    for (int loop = 0; loop < loopCount; loop++)
                    {
                        int maxRow = DeviceConfig.StationRowNum + 1;
                        if ((loopCount == 1))
                        {
                            if ((SurplusDrug > 0))
                            {
                                maxRow = SurplusDrug + 1;
                            }
                        }
                        else
                        {
                            if ((loop == (loopCount - 1)) && (SurplusDrug > 0))
                            {
                                maxRow = SurplusDrug + 1;
                            }
                        }
                        COneLoopStations oneLoop = (COneLoopStations)multyLoop.MultyLoopStation[loop].Clone();
                        oneLoop.UsedDrug = (DTO_adi_dic_drug)advice.Drugs[0].Clone();

                        for (int row = 0; row < maxRow; row++)
                        {
                            COneRowStation oneRow = (COneRowStation)oneLoop.MultyRowStation[row].Clone();
                            CSingleStation oneStation = (CSingleStation)oneRow.RowStations[col].Clone();
                            oneStation.adviceAndDrug = (CMS_AdviceAndDrug)l_advice[col].Clone();
                            oneStation.UsedFlag = 1;
                            int drugTypeCount = l_advice[col].Drugs.Count;

                            if (drugTypeCount > 1)
                            {
                                if (row == 0)
                                {
                                    //这一行是溶媒的数据
                                    oneStation.UsedDrug = (DTO_adi_dic_drug)l_advice[col].Drugs[0].Clone();
                                }
                                else
                                {
                                    oneStation.UsedDrug = (DTO_adi_dic_drug)ldrugs[loop * 4 + row - 1].Clone();
                                }
                            }
                            else
                            {
                                //只有一种药
                                oneStation.UsedDrug = (DTO_adi_dic_drug)l_advice[col].Drugs[0].Clone();
                            }

                            oneRow.UsedDrug = (DTO_adi_dic_drug)l_advice[col].Drugs[0].Clone();
                            if ((oneRow.UsedDrug.SOLVENT_FLAG == 0))
                            {
                                //非溶媒
                                if ((oneRow.UsedDrug.NEEDLE_TYPE == 1))
                                {
                                    //水针的整只抽取容积，等于瓶子的容积
                                    oneStation.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].IN_VOLUME = oneStation.UsedDrug.BOTTLE_VOLUME;
                                    oneRow.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].IN_VOLUME = oneRow.UsedDrug.BOTTLE_VOLUME;
                                }
                            }
                            else if (oneRow.UsedDrug.SOLVENT_FLAG == 2)
                            {
                                //专用溶媒,溶解水剂，瓶子的容积是药的容积，in_volume是专用溶媒的容积
                                if ((oneRow.UsedDrug.NEEDLE_TYPE == 1))
                                {
                                    oneStation.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].IN_VOLUME = oneStation.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].SolventVolume;
                                    oneRow.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].IN_VOLUME = oneStation.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].SolventVolume;
                                    if ((double)oneRow.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].SolventVolume + oneRow.UsedDrug.BOTTLE_VOLUME > DeviceConfig.InjectorMaxML)
                                    {
                                        //MessageBox.Show("专用溶媒，溶解水针，超出注射器容积，无法配药！");
                                        string str = "专用溶媒，溶解水针，超出注射器容积，无法配药！";
                                        Application.Current.Dispatcher.Invoke(
                                        (Action)delegate ()
                                        {
                                            _dialogService.ShowDialog("WarningDialog", new DialogParameters($"message={str}"), null);
                                        });
                                        return 0;
                                    }
                                }
                                else
                                {
                                    //专用溶媒，溶解粉针
                                    oneStation.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].IN_VOLUME = oneStation.UsedDrug.BOTTLE_VOLUME;
                                    oneRow.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].IN_VOLUME = oneRow.UsedDrug.BOTTLE_VOLUME;
                                    if (oneRow.UsedDrug.BOTTLE_VOLUME > DeviceConfig.InjectorMaxML)
                                    {
                                        //MessageBox.Show("专用溶媒，溶解水针，超出注射器容积，无法配药!");
                                        string str = "专用溶媒，溶解水针，超出注射器容积，无法配药!";
                                        Application.Current.Dispatcher.Invoke(
                                        (Action)delegate ()
                                        {
                                            _dialogService.ShowDialog("WarningDialog", new DialogParameters($"message={str}"), null);
                                        });
                                        return 0;
                                    }
                                }
                            }
                            if (0 == loop)
                            {
                                int ret = SetFzzMode(ref oneStation, advice, row);
                                if (0 == ret)
                                {
                                    return 0;
                                }
                            }
                            oneStation.ValidDrugCount = maxRow - 1;
                            oneRow.UsedSolvent = (DTO_adi_dic_drug)l_advice[col].Solvent.Clone();
                            oneRow.ValidRowCount = maxRow - 1;
                            oneRow.RowStations[col] = (CSingleStation)oneStation.Clone();

                            oneLoop.MultyRowStation[row] = (COneRowStation)oneRow.Clone();
                        }
                        multyLoop.MultyLoopStation[loop] = (COneLoopStations)oneLoop.Clone();
                    }
                }
            }
            return 1;
        }

        private int GetFirstCanUsedStationID()
        {
            for (int i = 0; i < advices.adviceAndDrugs.Count; i++)
            {
                if (advices.adviceAndDrugs[i].Enable == 1)
                {
                    return i;
                }
            }
            return 0;
        }

        protected List<DTO_adi_dic_drug> GetDrugs(List<DTO_adi_dic_drug> drugs, List<int> counts)
        {
            List<DTO_adi_dic_drug> ldrugs = new List<DTO_adi_dic_drug>();
            for (int loopindex = 0; loopindex < counts.Count; loopindex++)
            {
                int drugcount = counts[loopindex];
                for (int drugindex = 0; drugindex < drugcount; drugindex++)
                {
                    ldrugs.Add((DTO_adi_dic_drug)drugs[loopindex].Clone());
                }
            }
            return ldrugs;
        }

        public int HP_AnalysisFlie(ref CMultyLoopStations multyLoop)
        {
            //设备行数
            List<CMS_AdviceAndDrug> l_advice = advices.adviceAndDrugs;
            int ColCount = l_advice.Count;//获取有几列药品要充沛
            int firtCanUsedStationID = GetFirstCanUsedStationID();
            //第一步获取药品最大的数量
            int maxDrugCountOfAllCol = GetMaxDurgCount();
            int drugType = Convert.ToInt32(advices.adviceAndDrugs[firtCanUsedStationID].Drugs[0].SOLVENT_FLAG);
            if (2 == drugType)
            {
                //专用溶媒模式
                maxDrugCountOfAllCol = maxDrugCountOfAllCol * 2;
            }
            //第二步，初始化list变量,这个时候就已经决定了药循环配药几次
            InitilizeMulLoopRowColStation(ref multyLoop, maxDrugCountOfAllCol);
            for (int col = 0; col < ColCount; col++)
            {
                //循环每一列,也就是一个处方
                if (l_advice[col].Enable == 0)
                {
                    continue;
                }
                CMS_AdviceAndDrug advice = (CMS_AdviceAndDrug)l_advice[col].Clone();
                int DrugCountOfAdvice = 0;//当前医嘱中，药品的数量
                DrugCountOfAdvice = GetDrugCountOfCurrentAdvice(advice);//获取该医嘱中需要配置药品的总个数
                drugType = Convert.ToInt32(advice.Drugs[0].SOLVENT_FLAG);
                if (2 == drugType)
                {
                    //专用溶媒模式
                    DrugCountOfAdvice = DrugCountOfAdvice * 2;
                }
                int loopCount = DrugCountOfAdvice / DeviceConfig.StationRowNum;//需要的循环次数
                int SurplusDrug = DrugCountOfAdvice % DeviceConfig.StationRowNum;//最后一次循环药品的数量
                if (SurplusDrug > 0)
                {
                    loopCount = loopCount + 1;
                }

                multyLoop.UsedDrug = (DTO_adi_dic_drug)advice.Drugs[0].Clone();
                multyLoop.UsedSolvent = (DTO_adi_dic_drug)advice.Solvent.Clone();
                {
                    if (l_advice[col].Drugs.Count != l_advice[col].DrugCountOfOneType.Count)
                    {
                        //MessageBox.Show("配药医嘱解析失败！无法配药！");
                        string str = "配药医嘱解析失败！无法配药！";
                        Application.Current.Dispatcher.Invoke(
                        (Action)delegate ()
                        {
                            _dialogService.ShowDialog("WarningDialog", new DialogParameters($"message={str}"), null);
                        });
                        return 0;
                    }
                    List<DTO_adi_dic_drug> ldrugs = GetDrugs(l_advice[col].Drugs, l_advice[col].DrugCountOfOneType);
                    //同一列，每一行的药品都是一样的
                    for (int loop = 0; loop < loopCount; loop++)
                    {
                        int maxRow = DeviceConfig.StationRowNum + 1;
                        if ((loopCount == 1))
                        {
                            if ((SurplusDrug > 0))
                            {
                                maxRow = SurplusDrug + 1;
                            }
                        }
                        else
                        {
                            if ((loop == (loopCount - 1)) && (SurplusDrug > 0))
                            {
                                maxRow = SurplusDrug + 1;
                            }
                        }
                        COneLoopStations oneLoop = (COneLoopStations)multyLoop.MultyLoopStation[loop].Clone();
                        oneLoop.UsedDrug = (DTO_adi_dic_drug)advice.Drugs[0].Clone();

                        for (int row = 0; row < maxRow; row++)
                        {
                            COneRowStation oneRow = (COneRowStation)oneLoop.MultyRowStation[row].Clone();
                            CSingleStation oneStation = (CSingleStation)oneRow.RowStations[col].Clone();
                            oneStation.adviceAndDrug = (CMS_AdviceAndDrug)l_advice[col].Clone();
                            oneStation.UsedFlag = 1;
                            int drugTypeCount = l_advice[col].Drugs.Count;

                            if (drugTypeCount > 1)
                            {
                                if (row == 0)
                                {
                                    oneStation.UsedDrug = (DTO_adi_dic_drug)l_advice[col].Drugs[0].Clone();
                                }
                                else
                                {
                                    oneStation.UsedDrug = (DTO_adi_dic_drug)ldrugs[loop * 4 + row - 1].Clone();
                                }
                            }
                            else
                            {
                                //只有一种药
                                oneStation.UsedDrug = (DTO_adi_dic_drug)l_advice[col].Drugs[0].Clone();
                            }

                            oneRow.UsedDrug = (DTO_adi_dic_drug)l_advice[col].Drugs[0].Clone();
                            if ((oneRow.UsedDrug.SOLVENT_FLAG == 0))
                            {
                                //普通溶媒
                                if ((oneRow.UsedDrug.NEEDLE_TYPE == 1))
                                {
                                    //水针的整只抽取容积，等于瓶子的容积
                                    oneStation.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].IN_VOLUME = oneStation.UsedDrug.BOTTLE_VOLUME;
                                    oneRow.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].IN_VOLUME = oneRow.UsedDrug.BOTTLE_VOLUME;
                                }
                            }
                            else if (oneRow.UsedDrug.SOLVENT_FLAG == 2)
                            {
                                //专用溶媒,溶解水剂，瓶子的容积是药的容积，in_volume是专用溶媒的容积
                                if ((oneRow.UsedDrug.NEEDLE_TYPE == 1))
                                {
                                    oneStation.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].IN_VOLUME = oneStation.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].SolventVolume;
                                    oneRow.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].IN_VOLUME = oneStation.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].SolventVolume;
                                    if ((double)oneRow.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].SolventVolume + oneRow.UsedDrug.BOTTLE_VOLUME > DeviceConfig.InjectorMaxML)
                                    {
                                        //MessageBox.Show("专用溶媒，溶解水针，超出注射器容积，无法配药！");
                                        string str = "专用溶媒，溶解水针，超出注射器容积，无法配药！";
                                        Application.Current.Dispatcher.Invoke(
                                        (Action)delegate ()
                                        {
                                            _dialogService.ShowDialog("WarningDialog", new DialogParameters($"message={str}"), null);
                                        });
                                        return 0;
                                    }
                                }
                                else
                                {
                                    //专用溶媒，溶解粉针
                                    oneStation.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].IN_VOLUME = oneStation.UsedDrug.BOTTLE_VOLUME;
                                    oneRow.UsedDrug.flush_Groups[oneRow.UsedDrug.FlushGroupIndex].flush_Params[0].IN_VOLUME = oneRow.UsedDrug.BOTTLE_VOLUME;
                                    if (oneRow.UsedDrug.BOTTLE_VOLUME > DeviceConfig.InjectorMaxML)
                                    {
                                        //MessageBox.Show("专用溶媒，溶解水针，超出注射器容积，无法配药!");
                                        string str = "专用溶媒，溶解水针，超出注射器容积，无法配药!";
                                        Application.Current.Dispatcher.Invoke(
                                        (Action)delegate ()
                                        {
                                            _dialogService.ShowDialog("WarningDialog", new DialogParameters($"message={str}"), null);
                                        });
                                        return 0;
                                    }
                                }
                            }
                            if (0 == loop)
                            {
                                int ret = SetFzzMode(ref oneStation, advice, row);
                                if (0 == ret)
                                {
                                    return 0;
                                }
                            }
                            oneStation.ValidDrugCount = maxRow - 1;
                            oneRow.UsedSolvent = (DTO_adi_dic_drug)l_advice[col].Solvent.Clone();
                            oneRow.ValidRowCount = maxRow - 1;
                            oneRow.RowStations[col] = (CSingleStation)oneStation.Clone();

                            oneLoop.MultyRowStation[row] = (COneRowStation)oneRow.Clone();
                        }
                        multyLoop.MultyLoopStation[loop] = (COneLoopStations)oneLoop.Clone();
                    }
                }
            }
            return 1;
        }

        #endregion 整只配药模式

        protected int SetFzzMode(ref CSingleStation oneStation, CMS_AdviceAndDrug advice, int row)
        {
            if (advice.GetRealDose() > 0)
            {
                float allUsedDrugDose = Convert.ToSingle(advice.GetRealDose().ToString("F3"));
                float allDrugDose = Convert.ToSingle((advice.GetDose().ToString("F3")));
                if (allUsedDrugDose < allDrugDose)
                {
                    //非整只模式
                    float allInVolume = advice.Drugs[0].flush_Groups[0].flush_Params[0].fzzFlushInVolume;//注入西林瓶内的容积
                    float dose = (advice.adviceDrug[0].DOSE);
                    float fzz_realdose = (advice.adviceDrug[0].REALDOSE - (Convert.ToInt32((advice.adviceDrug[0].QUANTITY) - 1) * advice.adviceDrug[0].DOSE));//不是整只的实际剂量

                    //普通溶媒配药
                    if (advice.Drugs[0].SOLVENT_FLAG == 0)
                    {
                        //水针：普通溶媒
                        if ((advice.Drugs[0].NEEDLE_TYPE == 1))
                        {
                            //水针,单位转换成毫升
                            if (row == 1)
                            {
                                //非整只配药模式
                                oneStation.FeiZhengZhiMode = 1;
                                allInVolume = advice.Drugs[0].BOTTLE_VOLUME;
                                oneStation.fzz_OutVolume = (allInVolume * (fzz_realdose / dose));//计算实际要抽出的荣没量
                            }
                            else
                            {
                                //整只配药模式
                                oneStation.FeiZhengZhiMode = 0;
                            }
                        }
                        else
                        {
                            //粉针： 普通溶媒模式,
                            if (row == 1)
                            {
                                //非整只配药模式
                                oneStation.FeiZhengZhiMode = 1;
                                oneStation.fzz_OutVolume = (allInVolume * (fzz_realdose / dose));//计算实际要抽出的荣没量
                            }
                            else
                            {
                                //整只配药模式
                                oneStation.FeiZhengZhiMode = 0;
                            }
                        }
                    }
                    else
                    {
                        //专用溶媒配药
                        if (advice.Drugs[0].SOLVENT_FLAG == 2)
                        {
                            //这个是专用溶媒模式，第一行是专用溶媒液体，第二行是药
                            if ((row == 2))
                            {
                                //非整只配药模式
                                oneStation.FeiZhengZhiMode = 1;
                                //专用溶媒：粉针
                                if (Convert.ToInt32(advice.Drugs[0].NEEDLE_TYPE) == 0)
                                {
                                    //粉针
                                    oneStation.fzz_OutVolume = (allInVolume * (fzz_realdose / dose));//计算实际要抽出的荣没量
                                }
                                else
                                {
                                    //水针
                                    allInVolume = advice.Drugs[0].flush_Groups[0].flush_Params[0].fzzFlushInVolume + advice.Drugs[0].BOTTLE_VOLUME;//注入西林瓶内的容积
                                    oneStation.fzz_OutVolume = (allInVolume * (fzz_realdose / dose));//计算实际要抽出的荣没量
                                }
                            }
                            else
                            {
                                //整只配药模式
                                oneStation.FeiZhengZhiMode = 0;
                            }
                        }
                    }
                }
                else
                {
                    //整只配药模式
                    oneStation.FeiZhengZhiMode = 0;
                }
            }
            else
            {
                //整只配药模式
                oneStation.FeiZhengZhiMode = 0;
            }
            //这个地方不确定，水针的非整只抽出体积大于注射器容积如何解决，专用溶媒则直接退出
            if ((oneStation.fzz_OutVolume > DeviceConfig.InjectorMaxML) && (advice.Drugs[0].SOLVENT_FLAG == 2))
            {
                //MessageBox.Show("专用溶媒，溶解水针，超出注射器容积，无法配药！");
                string str = "专用溶媒，溶解水针，超出注射器容积，无法配药！";
                Application.Current.Dispatcher.Invoke(
                (Action)delegate ()
                {
                    _dialogService.ShowDialog("WarningDialog", new DialogParameters($"message={str}"), null);
                });
                return 0;
            }
            return 1;
        }

        #region 结余模式 ，医嘱解析

        public int Surplus_AnalysisFlie(ref Surplus_COneLoopStations Surplus_oneLoop)
        {
            int firtCanUsedStationID = GetFirstCanUsedStationID();
            //设备行数
            List<CMS_AdviceAndDrug> l_advice = new List<CMS_AdviceAndDrug>();
            for (int i = 0; i < advices.adviceAndDrugs.Count; i++)
            {
                l_advice.Add((CMS_AdviceAndDrug)advices.adviceAndDrugs[i].Clone());
            }
            int ColCount = l_advice.Count;//获取有几列药品要充沛
            COneLoopStations oneLoop = new COneLoopStations();
            //第二步，初始化list变量,这个时候就已经决定了药循环配药几次
            Surplus_InitilizeOneLoopRowColStation(ref oneLoop);

            for (int col = 0; col < ColCount; col++)
            {
                if (l_advice[col].Enable == 0)
                {
                    continue;
                }
                //循环每一列,也就是一个处方
                CMS_AdviceAndDrug advice = l_advice[col];

                advice.Drugs[0].flush_Groups[0].flush_Params[0].SolventVolume = advice.Drugs[0].flush_Groups[0].flush_Params[0].IN_VOLUME;//抽出的溶媒容积要等于冲刷容积
                int SolventCountOfAdvice = 0;//当前医嘱中，药品的数量
                SolventCountOfAdvice = Surplus_GetSolventCountOfCurrentAdvice(advice);//获取该医嘱中需要溶媒的的总袋数
                oneLoop.UsedDrug = (DTO_adi_dic_drug)advice.Drugs[0].Clone();
                oneLoop.UsedSolvent = (DTO_adi_dic_drug)advice.Solvent.Clone();
                Surplus_oneLoop.UsedDrug = (DTO_adi_dic_drug)advice.Drugs[0].Clone();
                Surplus_oneLoop.UsedSolvent = (DTO_adi_dic_drug)advice.Solvent.Clone();
                Surplus_oneLoop.adviceDrugDose = (double)advice.adviceDrug[0].DOSE;
                Surplus_oneLoop.adviceDrugRealDose = (double)advice.adviceDrug[0].REALDOSE;
                Surplus_oneLoop.NeedSolventCount[col] = SolventCountOfAdvice;//需要溶媒袋的个数
                Surplus_oneLoop.DrugDose[col] = (advice.Drugs[0].DOSE);
                //同一列，每一行的药品都是一样的
                for (int row = 0; row < 2; row++)
                {
                    COneRowStation oneRow = (COneRowStation)oneLoop.MultyRowStation[row].Clone();
                    CSingleStation oneStaion = (CSingleStation)oneRow.RowStations[col].Clone();
                    oneStaion.adviceAndDrug = (CMS_AdviceAndDrug)advice.Clone();
                    oneStaion.UsedFlag = 1;
                    double allInVolume = (double)advice.Drugs[0].flush_Groups[0].flush_Params[0].fzzFlushInVolume;//注入西林瓶内的容积
                    double dose = (double)(advice.adviceDrug[0].DOSE);
                    double realdose = (double)(advice.adviceDrug[0].REALDOSE);
                    oneStaion.fzz_OutVolume = (allInVolume * (realdose / dose));//计算实际要抽出的荣没量

                    oneRow.UsedDrug = (DTO_adi_dic_drug)advice.Drugs[0].Clone();
                    oneRow.UsedSolvent = (DTO_adi_dic_drug)advice.Solvent.Clone();
                    oneRow.RowStations[col] = oneStaion;
                    oneRow.RowStations[col].UsedDrug = (DTO_adi_dic_drug)advice.Drugs[0].Clone();//用于解决相同名称的药品，不同的剂量
                    oneLoop.MultyRowStation[row] = oneRow;
                }
                Surplus_oneLoop.OneLoopStation = oneLoop;
            }
            return 1;
        }

        protected int Surplus_GetMaxSolventCount()
        {
            List<CMS_AdviceAndDrug> l_advice = advices.adviceAndDrugs;
            int ColCount = l_advice.Count;//获取有几列药品要充沛
            //第一步获取药品最大的数量
            int MaxSolventCountOfAllCol = 0;
            for (int col = 0; col < ColCount; col++)
            {
                //循环每一列,也就是一个处方
                CMS_AdviceAndDrug advice = l_advice[col];
                int SolventCountOfCurrentAdvice = 0;
                SolventCountOfCurrentAdvice = Surplus_GetSolventCountOfCurrentAdvice(advice);//获取需要配置药品的总个数
                if (col == 0)
                {
                    MaxSolventCountOfAllCol = SolventCountOfCurrentAdvice;
                }
                else
                {
                    if (MaxSolventCountOfAllCol < SolventCountOfCurrentAdvice)
                    {
                        //如果当前的药品数量大于上一次记录的，则将当前的值保存
                        MaxSolventCountOfAllCol = SolventCountOfCurrentAdvice;
                    }
                }
            }
            return MaxSolventCountOfAllCol;
        }

        //获取医嘱药品数量
        protected int Surplus_GetSolventCountOfCurrentAdvice(CMS_AdviceAndDrug advcie)
        {
            //这种是，实际使用剂量，小于药品的实际剂量

            int SolventCount = Convert.ToInt32(Math.Floor((float)advcie.adviceDrug[0].DOSE / (float)advcie.adviceDrug[0].REALDOSE));
            return SolventCount;
        }

        protected void Surplus_InitilizeOneLoopRowColStation(ref COneLoopStations oneLoopStation)
        {
            double[] srowStaion = new double[MaxRowPointCount];
            srowStaion[0] = WorkPoint.X_StationPos0;//抽溶媒位置
            srowStaion[1] = WorkPoint.X_StationPos1;//小药位置1
            srowStaion[2] = WorkPoint.X_StationPos2;//小药位置2
            srowStaion[3] = WorkPoint.X_StationPos3;//小药位置3
            srowStaion[4] = WorkPoint.X_StationPos4;//小药位置4
            //初始化每个站台的X,Y坐标
            for (int row = 0; row < MaxRowPointCount; row++)
            {
                COneRowStation rowStation = Surplus_InitializeOneRow(srowStaion[row]);
                oneLoopStation.MultyRowStation[row] = rowStation;
            }
        }

        public COneRowStation Surplus_InitializeOneRow(double XPos)
        {
            COneRowStation rowStation = new COneRowStation();
            for (int col = 0; col < MaxColPointCount; col++)
            {
                //初始化一行数据
                CSingleStation lStation = new CSingleStation();
                Surplus_InitilizeSingleStation(ref lStation);
                rowStation.XPos = XPos;
                rowStation.RowStations[col] = lStation;
            }
            return rowStation;
        }

        //初始化一个站台信息
        protected void Surplus_InitilizeSingleStation(ref CSingleStation pStation)
        {
            //初始化每个站台的X,Y坐标
            CSingleStation lStation = new CSingleStation();
            lStation.UsedFlag = 0;
            lStation.fzz_OutVolume = 0;
            pStation = lStation;
        }

        #endregion 结余模式 ，医嘱解析
    }
}